% CALCULATE AVERAGE TRACES & RMSE'S OVER ALL VALID CYCLES

PlotNoisy.X = [];
PlotSmooth.X = [];

PlotNoisy.P = [];
PlotSmooth.P = [];

PlotNoisy.velnosac = [];
PlotSmooth.velnosac = [];

PlotNoisy.allerrsq = [];
PlotSmooth.allerrsq = [];

PlotNoisy.allerrsqocc = [];
PlotSmooth.allerrsqocc = [];

PlotNoisy.allerrsqfix = [];
PlotSmooth.allerrsqfix = [];

for n = 1:total_noisy
    if ismember(Noisy(n).no,omit) == 0  % is cycle valid?
        PlotNoisy.X           = [PlotNoisy.X, Noisy(n).X];
        PlotNoisy.P           = [PlotNoisy.P, Noisy(n).P];
        PlotNoisy.velnosac    = [PlotNoisy.velnosac, Noisy(n).vel];
        PlotNoisy.allerrsq    = [PlotNoisy.allerrsq; Noisy(n).errsq];
        PlotNoisy.allerrsqocc = [PlotNoisy.allerrsqocc; Noisy(n).errsqocc];
        PlotNoisy.allerrsqfix = [PlotNoisy.allerrsqfix; Noisy(n).errsqfix];
    end
end 
   
for s = 1:total_smooth
    if ismember(Smooth(s).no,omit) == 0  % is cycle valid?
        PlotSmooth.X           = [PlotSmooth.X, Smooth(s).X];
        PlotSmooth.P           = [PlotSmooth.P, Smooth(s).P];
        PlotSmooth.velnosac    = [PlotSmooth.velnosac, Smooth(s).vel];
        PlotSmooth.allerrsq    = [PlotSmooth.allerrsq; Smooth(s).errsq];
        PlotSmooth.allerrsqocc = [PlotSmooth.allerrsqocc; Smooth(s).errsqocc];
        PlotSmooth.allerrsqfix = [PlotSmooth.allerrsqfix; Smooth(s).errsqfix];
    end
end


PlotNoisy.mean = nanmean(PlotNoisy.X,2);
PlotSmooth.mean = nanmean(PlotSmooth.X,2);

PlotNoisy.pupil = nanmean(PlotNoisy.P,2);
PlotSmooth.pupil = nanmean(PlotSmooth.P,2);

PlotNoisy.time = 1:msPerCycle;
PlotSmooth.time = 1:msPerCycle;

PlotNoisy.RMSE = sqrt(nanmean(PlotNoisy.allerrsq));
PlotSmooth.RMSE = sqrt(nanmean(PlotSmooth.allerrsq));
Stats.RMSE(:,1) = [PlotNoisy.RMSE, PlotSmooth.RMSE];

PlotNoisy.RMSEocc = sqrt(nanmean(PlotNoisy.allerrsqocc));
PlotSmooth.RMSEocc = sqrt(nanmean(PlotSmooth.allerrsqocc));
Stats.RMSE(:,2) = [PlotNoisy.RMSEocc, PlotSmooth.RMSEocc];

PlotNoisy.RMSEfix = sqrt(nanmean(PlotNoisy.allerrsqfix));
PlotSmooth.RMSEfix = sqrt(nanmean(PlotSmooth.allerrsqfix));
Stats.RMSE(:,3) = [PlotNoisy.RMSEfix, PlotSmooth.RMSEfix];

      
% CALCULATE MEAN VELOCITIES EXCL SACCADES & SMOOTH

PlotNoisy.meanvelnosac = nanmean(PlotNoisy.velnosac,2);
PlotNoisy.meanvelnosac = smooth(PlotNoisy.meanvelnosac,smve);
PlotSmooth.meanvelnosac = nanmean(PlotSmooth.velnosac,2);
PlotSmooth.meanvelnosac = smooth(PlotSmooth.meanvelnosac,smve);


% CALCULATE MEAN VELOCITIES INCL SACCADES (smoothing sm = 100)

PlotNoisy.meanvel = velocity(PlotNoisy.mean,smvi,scnw,scnwcm,view_dist);
PlotSmooth.meanvel = velocity(PlotSmooth.mean,smvi,scnw,scnwcm,view_dist);


% CALCULATE LAGS

msPerhCycle = floor(msPerCycle/2);

PlotNoisy.lag(1:msPerhCycle,1) = target22.one(1:msPerhCycle,1)...
                                 - PlotNoisy.mean(1:msPerhCycle,1);
PlotNoisy.lag(msPerhCycle+1:msPerCycle,1) = ...
    PlotNoisy.mean(msPerhCycle+1:msPerCycle,1) - ...
    target22.one(msPerhCycle+1:msPerCycle,1);

PlotSmooth.lag(1:msPerhCycle,1) = target22.one(1:msPerhCycle,1)...
                                  - PlotSmooth.mean(1:msPerhCycle,1);
PlotSmooth.lag(msPerhCycle+1:msPerCycle,1) = ...
    PlotSmooth.mean(msPerhCycle+1:msPerCycle,1) - ...
    target22.one(msPerhCycle+1:msPerCycle,1);